package leetCode.thread.leetCode1114;

/**
 * 普通while配合原子变量实现按序打印,自旋操作
 */
public class Foo1 implements Foo {

    volatile boolean  first = false;
    volatile boolean second = false;

    public Foo1() {
    }

    @Override
    public void first(Runnable printFirst) throws InterruptedException {
        while(true){
            if(!first&&!second){
                printFirst.run();
                first = true;
                break;
            }
        }
    }

    @Override
    public void second(Runnable printSecond) throws InterruptedException {
        while(true) {
            if(first&&!second){
                printSecond.run();
                second = true;
                break;
            }
        }
    }

    @Override
    public void third(Runnable printThird) throws InterruptedException {
        while(true){
            if(first&&second){
                printThird.run();
                break;
            }
        }
    }

}
